package com.qf.handler.in;

import com.qf.entity.Msg;
import com.qf.util.ChannelMap;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

/**
 * 客户端信息注册
 */
@Component
@ChannelHandler.Sharable
@Order(3000)
public class RegisterChannelHandler extends SimpleChannelInboundHandler<Msg> {

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, Msg msg) throws Exception {
         if (msg.getType() == 1) {
             //注册当前用户信息
             Integer uid = msg.getFrom();

             //将用户id 和 对应的channel对象缓存起来，供后续使用
             ChannelMap.put(uid, ctx.channel());
         } else {
             ctx.fireChannelRead(msg);
         }
    }
}
